package com.tool.cn.util;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 
 * @author wangzonghui
 * @date 2021-09-06 15:49:59
 * @Description 线程工具类
 */
public final class ThreadUtil {

	/**
	 * 本机物理线程数*2
	 * @return
	 */
	public static int threadSize() {
		return Runtime.getRuntime().availableProcessors()*2;
	}
	
	/**
	 * 创建最优线程池
	 * @return 
	 */
	public static ThreadPoolExecutor createPool() {
		return new ThreadPoolExecutor(threadSize(), threadSize(),60L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
	}
	
	
	/**
	 * 创建线程池，线程空闲60秒销毁
	 * @param poolSize 线程池大小
	 * @return
	 */
	public static ThreadPoolExecutor createPool(int poolSize) {
		return new ThreadPoolExecutor(poolSize, poolSize,60L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
	}
	
	/**
	 * 创建线程池
	 * @param poolSize 线程池大小
	 * @param aliveSecond 空闲线程存活秒数
	 * @return
	 */
	public static ThreadPoolExecutor createPool(int poolSize,int aliveSecond) {
		return new ThreadPoolExecutor(poolSize, poolSize,aliveSecond, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
	}
}
